const News = require('../module/news')
const {getCurrentTime} = require('../utils')
// 更新新闻的处理函数
exports.updateNews = async (req, res) => {
    let _id = req.body._id
    delete req.body['_id']
    req.body['update_at'] = getCurrentTime()
    const u = await News.updateOne({_id}, req.body)
    console.log('uuuuuuuuuuuuuuuu', u)
    return res.sendJson('更新新闻ok', 0, req.body)
}

// 新增新闻的处理函数
exports.addNews = async (req, res) => {
    req.body['create_at'] = getCurrentTime()
    console.log(req.body)
    const u = await new News(req.body).save()
    const id = u._id.toString() || null
    if (id) {
        return res.sendJson('新增新闻成功', 0, {id})
    }
    return res.sendJson('新增新闻失败', 1)
}

exports.newsList = async (req, res) => {
    console.log(req.query)
    let page = req.query.page || 1
    let size = req.query.size || 10
    let skip = (page - 1) * size
    const lst = await News.find({}).limit(size).skip(skip).sort("-created").select("-__v");
    console.log(lst)
    return res.sendJson('ok', 0, lst)
}

exports.newsInfo = async (req, res) => {
    console.log(req.params)
    const lst = await News.findOne({_id: req.params.id});
    console.log(lst)
    return res.sendJson('ok', 0, lst)
}

exports.delNews = async (req, res) => {
    console.log(req.params)
    const lst = await News.deleteOne({_id: req.params.id});
    console.log(lst)
    return res.sendJson('ok', 0, lst)
}
